<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import * as echarts from "echarts";
require("echarts/theme/macarons"); // echarts theme
import resize from "../mixins/resize";

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "260px",
    },
    chartdata: {
      type: Array,
    },
  },
  data() {
    return {
      chart: null,
      total:0,
    };
  },
  mounted() {
    this.$nextTick(() => {
      var that = this;
      var data = [];
      for (var i = 0; i < that.chartdata.length; i++) {
        data.push({
          name: that.chartdata[i].workStatus,
          value: that.chartdata[i].cnt,
        });
        this.total=this.total+that.chartdata[i].cnt;
      }
      this.initChart(data);
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart(data) {
      this.chart = echarts.init(this.$el, "macarons");

      this.chart.setOption({
        title: {
          text: "危险作业状态数量统计",
          left: "left",
          padding: [
            10, // 上
            10, // 右
            10, // 下
            10, // 左
          ],
          textStyle: { color: "#606266", fontSize: "14" },
        },
        tooltip: {
          trigger: "item",
        },
        legend: {
          orient: "vertical",
          top: "100px",
          left: "70%",
          textStyle: {
            //数值样式
            color: "#606266", //字体颜色
          },
        },
        series: [
          {
            name: "数据",
            type: "pie",
            radius: ["30%", "60%"],
            center: ["36%", "50%"],
            avoidLabelOverlap: false,
            label: {
              normal: {
                show: true,
                position: "center",
                color: "#4c4a4a",
                formatter:
                  "总数:"+this.total,
                rich: {
                  total: {
                    fontSize: 35,
                    fontFamily: "微软雅黑",
                    color: "#454c5c",
                  },
                  active: {
                    fontFamily: "微软雅黑",
                    fontSize: 16,
                    color: "#6c7a89",
                    lineHeight: 30,
                  },
                },
              },
              emphasis: {
                //中间文字显示
                show: true,
              },
            },
            lableLine: {
              normal: {
                show: false,
              },
              emphasis: {
                show: true,
              },
              tooltip: {
                show: false,
              },
            },
            emphasis: {
              label: {
                show: true,
                fontSize: "24",
                fontWeight: "bold",
              },
            },
            data: data,
          },
        ],
      });
    },
  },
};
</script>
